﻿using DataShow.codeModel;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DataShow.forecasters
{
    public partial class Forecaster : System.Web.UI.Page
    {

        #region 页面加载事件
        string type = ""; //传过来的表名
        bool isOptimize; //是否是优化
        protected string UserName;//预报员名字
        protected bool isForecaster = false;
        protected string ForecasterURL = "Index.aspx";
        protected string IndexURL = "/Index.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            bool isadmin = Convert.ToBoolean(Session["isadmin"]);
            if(isadmin)
            {
                btn_add.Visible = false;
            }
            isForecaster = Request.QueryString["isf"] != null; //
            if (isForecaster == true)
            {
                ForecasterURL = "/ForecasterIndex.aspx";
                IndexURL = "/ForecasterIndex.aspx";
            }
            else
            {
                btn_search2.Visible = false;
            }
            type = Request.QueryString["type"];
            if (string.IsNullOrEmpty(type))
            {
                Response.Redirect("Index.aspx");
                Response.End();
            }
            if (string.IsNullOrEmpty(Request.QueryString["isoptimize"]))
            {
                isOptimize = false;
            }
            else
            {
                isOptimize = true;
            }
            this.anp.PageSize = Convert.ToInt32(ConfigurationManager.AppSettings["pagesize"]);
            if (!IsPostBack)
            {
                time_begin.Text = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
                time_end.Text = DateTime.Now.ToString("yyyy-MM-dd");
                Common.CheckAllCheckbox(cbl_types); cbx_type_all.Checked = true;
                Common.CheckAllCheckbox(cbl_sites); cbx_site_all.Checked = true;
                Common.CheckAllCheckbox(cbl_sx); cbx_sx_all.Checked = true;
                Common.CheckAllCheckbox(cbl_point); cbx_point_all.Checked = true;
                Search();
                if (Request.QueryString["save"] == "true")
                {
                    Common.RegisterScriptAtPageLast(Page, "alert('保存成功！')");
                }
            }
            string sql = "select name from account_forecaster where username='"+type.Substring(0,type.IndexOf("_UFore"))+"'";
            UserName = new codeDAL.SqlHelper().ExecuteScalar(sql).ToString();
            
        }
        #endregion

        #region 搜索按钮事件
        /// <summary>
        /// 搜索按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_search_Click(object sender, EventArgs e)
        {
            Search();
        }
        #endregion


        #region 搜索
        /// <summary>
        /// 搜索
        /// </summary>
        private void Search(int index = 1)
        {
            DateTime Time_Begin = DateTime.Parse(time_begin.Text.Trim());
            DateTime Time_End = DateTime.Parse(time_end.Text.Trim());

            List<Station> Stations = new List<Station>();
            foreach (ListItem item in cbl_sites.Items)
            {
                if (item.Selected)
                {
                    Stations.Add((Station)Enum.Parse(typeof(Station), item.Value));
                }
            }
            List<Element> Elements = new List<Element>();
            foreach (ListItem item in cbl_types.Items)
            {
                if (item.Selected)
                {
                    Elements.Add((Element)Enum.Parse(typeof(Element), item.Value));
                }
            }

            List<ForeSpanType> FST = new List<ForeSpanType>();
            foreach (ListItem item in cbl_sx.Items)
            {
                if (item.Selected)
                {
                    FST.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), item.Value));
                }
            }

            List<SendPoint> SendPoints = new List<SendPoint>();
            foreach (ListItem item in cbl_point.Items)
            {
                if (item.Selected)
                {
                    SendPoints.Add((SendPoint)Enum.Parse(typeof(SendPoint), item.Value));
                }
            }
            if (Stations.Count == 0)
            {
                anp.Visible = false;
                gv_result.Visible = false;
                div_nodata.Visible = true;
                return;
            }

            anp.CurrentPageIndex = index;
            int pagesize = anp.PageSize;
            int pageindex = anp.CurrentPageIndex;

            var lists = new codeBLL.ForecastBLL().Search(pagesize/Stations.Count, pageindex, type, Time_Begin, Time_End, Elements, FST, Stations, SendPoints, isOptimize);
            gv_result.DataSource = lists;
            gv_result.DataBind();

            anp.RecordCount = new codeBLL.ForecastBLL().CalculateCount(type, Time_Begin, Time_End, Elements, FST, Stations, SendPoints, isOptimize)*Stations.Count;
            if (anp.RecordCount == 0)
            {
                gv_result.Visible = false;
                anp.Visible = false;
                div_nodata.Visible = true;
            }
            else
            {
                gv_result.Visible = true;
                anp.Visible = true;
                div_nodata.Visible = false;
            }
        }
        #endregion


        #region 处理全选

        protected void cbx_type_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_types.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }


        protected void cbx_point_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_point.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }


        protected void cbx_site_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_sites.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }

        protected void cbx_sx_all_CheckedChanged(object sender, EventArgs e)
        {
            foreach (ListItem items in cbl_sx.Items)
            {
                if ((sender as CheckBox).Checked)
                {
                    items.Selected = true;
                }
                else
                {
                    items.Selected = false;
                }
            }
        }

        #endregion


        #region 更新全选按钮，并按已选择条件搜索

        protected void cbl_point_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_point.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_point_all.Checked = false;
            }
        }

        protected void cbl_types_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_sites.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_site_all.Checked = false;
            }
            Search();
        }

        protected void cbl_sites_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_sites.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_site_all.Checked = false;
            }
        }




        protected void cbl_sx_SelectedIndexChanged(object sender, EventArgs e)
        {
            var checkAll = true;
            foreach (ListItem item in cbl_sx.Items)
            {
                if (!item.Selected)
                {
                    checkAll = false;
                }
            }
            if (!checkAll)
            {
                cbx_sx_all.Checked = false;
            }
        }

        #endregion

        #region 处理翻页事件
        /// <summary>
        /// 处理翻页事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void anp_PageChanged(object sender, EventArgs e)
        {
            Search(anp.CurrentPageIndex);
        }
        #endregion

        #region 存储
        /// <summary>
        /// 存储
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_save_Click(object sender, EventArgs e)
        {
            DateTime time = DateTime.Parse(txt_add_time.Text.Trim());
            string send_point = ddl_send_point.SelectedValue;
            List<PanelItem> items = new List<PanelItem>();
            PanelItem lyg = und_lyg.GetPanelValue();
            PanelItem gy = und_gy.GetPanelValue();
            PanelItem dh = und_dh.GetPanelValue();
            PanelItem guanyun = und_guanyun.GetPanelValue();
            PanelItem gn = und_gn.GetPanelValue();
            items.Add(lyg);
            items.Add(gy);
            items.Add(dh);
            items.Add(guanyun);
            items.Add(gn);
            
            UserAddData FD00_24 = new UserAddData();
            foreach (var item in items)
            {
                FD00_24.Date = time.ToString("YYYY-mm-dd");
                FD00_24.Semitype = "Auto";
                FD00_24.SendPoint = send_point;
                FD00_24.Span = "FD00_24";
                FD00_24.TableName = "";
            }

            
            

        }
        /// <summary>
        /// 发报时刻
        /// </summary>
        /// <param name="sendPoint"></param>
        /// <returns></returns>
        private string GetClock(string sendPoint)
        {
            switch (sendPoint)
            {
                case "H10": return "H08";
                case "H16": return "H20";
                case "H06": return "H08";
                default: return "";
            }
        }
        #endregion

        protected void btn_add_Click(object sender, EventArgs e)
        {
            Response.Redirect("add.aspx?type=" + type);
            Response.End();
        }

    }
}